package com.moyou.weichat;

import android.content.Intent;
import android.media.AudioManager;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.moyou.weichat.bean.MessageNotificationBean;
import com.moyou.weichat.chat.ChatManger;
import com.moyou.weichat.fragment.UserFragment;
import com.moyou.weichat.listener.ChatEMMessageListener;
import com.moyou.weichat.listener.ChatMessageCallBack;
import com.moyou.weichat.mvp.presenter.MainPresenter;
import com.moyou.weichat.mvp.view.MainView;
import com.moyou.weichat.utils.ChatUtils;
import com.moyou.weichat.utils.ConstantUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.Bind;

public class MainActivity extends BaseActivity<MainPresenter> implements ChatMessageCallBack ,MainView{
    @Bind(R.id.main_tab_host)
    public  FragmentTabHost tabHost;
    @Bind(R.id.activity_main)
    CoordinatorLayout grCoordinatorLayout;
    private ChatEMMessageListener chatEMMessageListener;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initTitle() {
//        EventBus.getDefault().register(this);
    }

    @Override
    protected void initListener() {
        EMClient.getInstance().chatManager().addMessageListener(chatEMMessageListener);
    }

    @Override
    protected void initEventAndData() {
        mPresenter.initTabView(this,tabHost);//初始化TabHost
        mPresenter.initFragment(tabHost,mContext); //初始化Fragemnt
        chatEMMessageListener = new ChatEMMessageListener(this); //聊天的今天消息
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.LEFT;
    }

    @Override
    protected void createPresenter() {
        mPresenter = new MainPresenter(this);
    }


    @Override
    public void onMessageReceived(List<EMMessage> messages) {
        //1.这里需要去刷新聊天的列表！
        MessageNotificationBean messageNotificationBean =  new MessageNotificationBean();
        messageNotificationBean.setMessageType(ConstantUtils.CHAT_NOTIFICATION_NUMBER);
        EventBus.getDefault().post(messageNotificationBean);
        //2.还需要更改地步的未读消息数量
        int messageNumber = ChatManger.getChatManger().getUnreadMsgCountTotal();
        mPresenter.setMessageNumber(messageNumber);
        //3.还需要进行通知通知和播放提示音乐
        for (int i = 0; i <messages.size() ; i++) {
           String userName = messages.get(i).conversationId();
            String content = ((EMTextMessageBody) messages.get(i).getBody()).getMessage();
            ChatUtils.getChatUtils(mContext).showNotification(userName, content);
        }
    }

    @Override
    public void onCmdMessageReceived() {

    }

    @Override
    public void onMessageRead() {

    }

    @Override
    public void onMessageDelivered() {

    }

    @Override
    public void onMessageRecalled() {

    }

    @Override
    public void onMessageChanged() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //接受消息在销毁activity 的时候需要移除
        EMClient.getInstance().chatManager().removeMessageListener(chatEMMessageListener);
    }
    //更改底部选项的消息数量
    @Override
    public void setChangeMessageNbumber(TextView tvMessageNbumber ,int messageNumber) {
        //判断
       MainActivity.this.runOnUiThread(() -> {
            if ( messageNumber > 0){
                tvMessageNbumber.setVisibility(View.VISIBLE);
                tvMessageNbumber.setText(messageNumber+"");
            }else{
                tvMessageNbumber.setVisibility(View.GONE);
            }
        });
    }

//    //刷新底部的未读消息数
//    @Subscribe
//    public void setChatMarkMessageNumber( MessageNotificationBean messageNotificationBean){
//        MainActivity.this.runOnUiThread(() -> {
//            if (messageNotificationBean.getMessageType() == ConstantUtils.CHAT_NOTIFICATION_REFSH) {
//                int chat_number = ChatManger.getChatManger().getUnreadMsgCountTotal();
//                mPresenter.setMessageNumber(chat_number);
//            }
//        });
//    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUnreadLabel();
    }
    //获取维度消息总数
    private void updateUnreadLabel() {
        int chat_number = ChatManger.getChatManger().getUnreadMsgCountTotal();
        mPresenter.setMessageNumber(chat_number);
    }
}
